package helper;

import org.mybeans.dao.DAOException;

import model.PaymentDAO;
import model.TransactionDAO;
import databeans.Customer;
import databeans.Payment;
import databeans.Transaction;

public class Balance {
    
    private static TransactionDAO transactionDAO;
    private static PaymentDAO paymentDAO;
    
    /*
     * What i need
     * - knowing how much available balance does customer has
     * - knowing how much of customer fund is locked for transaction
     * - knowing how much shares is currently at pending
     * 
     */
    
    public static long getCustomerPendingTransaction(Customer customer) {
        int customerID = customer.getCustomerID();
        System.out.println(customerID + " is cusotmerID");
        long result = 0000;
        
        Transaction [] transactionList = null;
        
        try {
            transactionDAO = new TransactionDAO();
            
            //get from buy sell fund
            
            transactionList = transactionDAO.getPendingTransactions(customerID);
            
            if(transactionList == null){
                System.out.println("Transaction list is null");
                
            } else {
                System.out.println("Transaction list has length of " + transactionList.length);
                
            }
            
            
        } catch (DAOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } 
 
        if (transactionList == null) {
            result = 000;
        } else {
            for (int i = 0; i < transactionList.length; i++){
                //System.out.println("Transactionlist " + i + " has amount " + transactionList[i].getAmount() );
                //System.out.println("Transactionlist " + i + " has amountstring " + Double.toString(transactionList[i].getAmount()) );
                result = result + transactionList[i].getAmount(); //for selling shares, the amount will be zero anywawy
            }
        }
        
        Payment [] paymentList = null;
        try {
            paymentDAO = new PaymentDAO();
            paymentList = paymentDAO.getPayments(customerID);
            
            if (paymentList == null) {
                System.out.println("Payment list is null");
            } else {

                System.out.println("Payment list has length of " + paymentList.length);
            }
        } catch (DAOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Payment list for customer ID " + customer.getCustomerID() + " is " + paymentList.length);
        if (paymentList == null) {
            result = result + 000;
        } else {
            for (int i = 0; i < paymentList.length; i ++ ){
                System.out.println("Payment " + paymentList[i].getTransactionType() + " and status " + paymentList[i].isExecuted());
                if (paymentList[i].getTransactionType().equals(Transaction.CHECK_REQUEST) && !paymentList[i].isExecuted()){
                    result = result + paymentList[i].getAmount();
                }
            }
            
        }
        
        System.out.println("Result is " + result);

        return result;
    }
    
    public static long getCustomerAvailableCash(Customer customer) throws DAOException{
        long customerMoney = 0;

        long pendingTransaction = Balance.getCustomerPendingTransaction(customer);
        
        long customerAvailableCash = customer.getCash() - pendingTransaction;
        return customerAvailableCash;
    }
    
    //return amount of pending position
    public static long getPendingPosition(int fundID, int customerID){
        
        long result = 0000;
        
        Transaction [] transactionList = null;
        
        try {
            transactionDAO = new TransactionDAO();
            transactionList = transactionDAO.getPendingTransactions(customerID);
            if(transactionList == null){
                System.out.println("Transaction list is null");
            } else {
                System.out.println("Transaction list has length of " + transactionList.length);
            }

        } catch (DAOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        

        if (transactionList == null) {
            result = 000;
        } else {
            for (int i = 0; i < transactionList.length; i++){
                //System.out.println("Transactionlist " + i + " has amount " + transactionList[i].getAmount() );
                //System.out.println("Transactionlist " + i + " has amountstring " + Double.toString(transactionList[i].getAmount()) );
                if (transactionList[i].getFundID() == fundID){
                    result = result + transactionList[i].getShares(); //for selling shares, the amount will be zero anywawy
                }
            }
        }
        
        return result; 
        
    }

}
